package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.pojo.Category;
import com.itheima.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.itheima.common.R.success;

/*分类管理*/
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * <p>Description: 添加菜品分类/套餐</p>
     *
     * @author liuyiming
     * @date 2022-10-09 11:37
     * @version 1.0.0
     */
    @PostMapping
    @CacheEvict(value = "categoryCache", allEntries = true)
    public R<String> add(@RequestBody Category category) {
        log.info("添加菜品分类/套餐");
        categoryService.save(category);
        return R.success("添加成功");
    }

    /**
     * <p>Description: 分页查询分类</p>
     *
     * @author liuyiming
     * @date 2022-10-09 11:54
     * @version 1.0.0
     */
    @GetMapping("/page")
    @Cacheable(value = "categoryCache")
    public R<Page> page(int page, int pageSize) {
        log.info("菜品分类分页查询page={},pageSize={}", page, pageSize);
        //分页构造器
        Page<Category> pageInfo = new Page(page, pageSize);
        //条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //根据sort字段正序排序
        queryWrapper.orderByAsc(Category::getSort);
        categoryService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * <p>Description: 根据ID逻辑删除分类</p>
     *
     * @author liuyiming
     * @date 2022-10-09 12:06
     * @version 1.0.0
     */
    @DeleteMapping
    @CacheEvict(value = "categoryCache", allEntries = true)
    public R<String> delete(Long id) {
        log.info("逻辑删除分类{}", id);
        categoryService.remove(id);
        return R.success("删除分类成功");
    }

    /**
     * <p>Description: 根据ID修改分类</p>
     *
     * @author liuyiming
     * @date 2022-10-09 15:34
     * @version 1.0.0
     */
    @PutMapping
    @CacheEvict(value = "categoryCache", allEntries = true)
    public R<String> update(@RequestBody Category category) {
        log.info("修改分类信息{}", category);
        categoryService.updateById(category);
        return R.success("分类修改成功");
    }

    /**
     * <p>Description: 查询所有菜品分类</p>
     *
     * @author liuyiming
     * @date 2022-10-10 9:57
     * @version 1.0.0
     */
    @GetMapping("/list")
    @Cacheable(value = "categoryCache")
    public R<List<Category>> list(Category category) {
        log.info("查询所有菜品分类");
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(category.getType() != null,Category::getType,category.getType());
        queryWrapper.orderByAsc(Category::getSort);
        List<Category> list = categoryService.list(queryWrapper);
        return R.success(list);
    }

}
